package cn.com.digitalnet.ad.web;

import java.util.List;

import javax.servlet.ServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.com.digitalnet.ad.base.CrudController;
import cn.com.digitalnet.ad.entity.Permission;
import cn.com.digitalnet.ad.entity.Role;
import cn.com.digitalnet.ad.service.PermissionService;
import cn.com.digitalnet.ad.service.RoleService;

import com.google.common.collect.Lists;

@RequiresRoles("Admin")
@Controller
@RequestMapping(value = "/role")
public class RoleController extends CrudController<Role>{

	private static final String PAGE_SIZE = "10";

	{
		sortTypes.put("id", "自动");
		sortTypes.put("name", "名称");
		sortTypes.put("isenabled", "状态");
	}

	@Autowired
	private RoleService service;
	@Autowired
	private PermissionService permissionService;

	@Override
	protected RoleService service() {
		return service;
	}

	@Override
	protected String english() {
		return "role";
	}

	@Override
	protected String chinese() {
		return "角色";
	}

	@Override
	protected String parentMenu() {
		return "";
	}

	@RequestMapping(method = RequestMethod.GET)
	public String list(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			@RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "sortType", defaultValue = "auto") String sortType,
			@RequestParam(value = "sortDire", defaultValue = "auto") String sortDire,
			Model model,
			ServletRequest request) {
		return standardList(pageNumber, pageSize, sortType, sortDire, model, request);
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		List<Permission> allPermissions = permissionService.findEnabledAll();

		List<List<Permission>> permissions = Lists.newArrayList();
		List<Permission> currentGroup = Lists.newArrayList();
		String currentPrefix = "";
		for(Permission p : allPermissions){
			String prefix = p.getCode().split(":")[0];
			if(currentPrefix.equals("") || prefix.equals(currentPrefix)){
				//...
			}else{
				permissions.add(currentGroup);
				currentGroup = Lists.newArrayList();
			}
			currentGroup.add(p);
			currentPrefix = prefix;
		}
		permissions.add(currentGroup);
		
		model.addAttribute("permissions", permissions);
		
		return standardCreateForm(model);
	}

	///测试使用shiro权限控制方法
	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(@Valid Role newRole,@RequestParam("permissions") String[] permissions, RedirectAttributes redirectAttributes) {
		// bind permissionList
		newRole.getPermissionList().clear();
		if(ArrayUtils.isNotEmpty(permissions)){
			for (String permissionId : permissions) {
				Permission p = new Permission(Long.parseLong(permissionId));
				newRole.getPermissionList().add(p);
			}
		}

		return standardCreate(newRole, redirectAttributes);
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model model) {
		List<Permission> allPermissions = permissionService.findEnabledAll();
		
		List<List<Permission>> permissions = Lists.newArrayList();
		List<Permission> currentGroup = Lists.newArrayList();
		String currentPrefix = "";
		for(Permission p : allPermissions){
			String prefix = p.getCode().split(":")[0];
			if(currentPrefix.equals("") || prefix.equals(currentPrefix)){
				//...
			}else{
				permissions.add(currentGroup);
				currentGroup = Lists.newArrayList();
			}
			currentGroup.add(p);
			currentPrefix = prefix;
		}
		permissions.add(currentGroup);
		
		model.addAttribute("permissions", permissions);
		
		return this.standardUpdateForm(id, model);
	}

	@Override
	protected Role findEntityForUpdateForm(Long id){
		return service().findOneWithPermissions(id);
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@Valid @ModelAttribute("role") Role role,@RequestParam("permissions") String[] permissions, RedirectAttributes redirectAttributes) {
		// bind permissionList
		role.getPermissionList().clear();
		if(ArrayUtils.isNotEmpty(permissions)){
			for (String permissionId : permissions) {
				Permission p = new Permission(Long.parseLong(permissionId));
				role.getPermissionList().add(p);
			}
		}
		
		return standardUpdate(role, redirectAttributes);
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
		return standardDelete(id, redirectAttributes);
	}

	@ModelAttribute
	public void getEntity(@RequestParam(value = "id", defaultValue = "-1") Long id, Model model) {
		standardGetEntity(id, model);
	}

	@RequestMapping(value = "publish/{id}", method = RequestMethod.GET)
	public String publish(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
		return standardSubmit(id, redirectAttributes);
	}

}
